// pages/clockList/clockList.js
import __config from '../../config/env.js'

import todo from '../../components/v2/plugins/todo'
import selectable from '../../components/v2/plugins/selectable'
import solarLunar from '../../components/v2/plugins/solarLunar/index'
import timeRange from '../../components/v2/plugins/time-range'
import week from '../../components/v2/plugins/week'
import holidays from '../../components/v2/plugins/holidays/index'
import plugin from '../../components/v2/plugins/index'

plugin
  .use(todo)
  .use(solarLunar)
  .use(selectable)
  .use(week)
  .use(timeRange)
  .use(holidays)


const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:__config.basePath,
    onSelected:-1,
    year:'',
    month:'',
    days:['可选'],
    day:'可选',
    shopName:'',
    clolist:[],
    CustomBar:app.globalData.CustomBar,
    jinDu:0,
    weiDu:0,

    startdate:'',
    startdateobj: null,
    enddate:'',
    enddateobj:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.setDate()
    //this.search()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const calendar = this.selectComponent('#calendar').calendar
    //calendar.setSelectedDates()
  },
  afterTapDate(e) {
    const calendar = this.selectComponent('#calendar').calendar

    if(this.data.startdate == ''){
      let m = e.detail.month
      if(m < 10){
        m = '0' + m
      }
      let d = e.detail.date
      if(d < 10){
        d = '0' + d
      }

      this.data.startdate = e.detail.year + '-' + m + '-' + d
      this.data.startdateobj = e.detail
      console.log(1)
    }else if(this.data.enddate == ''){
      let m = e.detail.month
      if(m < 10){
        m = '0' + m
      }
      let d = e.detail.date
      if(d < 10){
        d = '0' + d
      }

      this.data.enddate = e.detail.year + '-' + m + '-' + d
      this.data.enddateobj = e.detail

      if(this.data.enddate < this.data.startdate){
        let tempdate = this.data.startdate;
        let tempdateobj = this.data.startdateobj;

        this.data.startdate = this.data.enddate;
        this.data.startdateobj = this.data.enddateobj;
        this.data.enddate = tempdate;
        this.data.enddateobj = tempdateobj;
      }

      console.log(2)
    }else{
      let m = e.detail.month
      if(m < 10){
        m = '0' + m
      }
      let d = e.detail.date
      if(d < 10){
        d = '0' + d
      }

      this.data.startdate = e.detail.year + '-' + m + '-' + d
      this.data.startdateobj = e.detail
      this.data.enddate = ''
      this.data.enddateobj = null

      console.log(3)
    }

    calendar.cancelSelectedDates()
    console.log([this.data.startdateobj, this.data.enddateobj||{}]);
    if(this.data.enddate == ''){
      calendar.setSelectedDates([this.data.startdateobj])
    }else{
      calendar.chooseDateArea([this.data.startdate, this.data.enddate])
    }

    if(this.data.startdate != '' && this.data.enddate != ''){
      this.search2()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setDate(){
    let date = new Date();
    this.setData({
      year:date.getFullYear(),
      month:date.getMonth() + 1 > 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
    })
    this.updateDate();
  },
  inpShopName:function(e){
    this.setData({
      shopName:e.detail.value
    })
  },
  search2:function(){
    let data = {startdate:this.data.startdate,enddate: this.data.enddate}
    app.api.getClocklist(data).then(res => {
      console.log(res);
      if(res.code == 200){
        this.setData({
          clolist : res.rows,
          onSelected:-1
        })
      }
    })
  },
  search:function(){
    let data = {}
    if(this.data.day == '可选'){
      let startdate = this.data.year + '-' + this.data.month + '-' + '01'
      let e_date = new Date(this.data.year,this.data.month)
      e_date.setMonth(e_date.getMonth() + 1)
      let m = e_date.getMonth()
      if(m < 10){
        m = '0' + m
      }
      let enddate = e_date.getFullYear() + '-' + m + '-' + '01'
      data = {startdate: startdate,enddate: enddate}
    }else{
      let startdate  = this.data.day + '';
      startdate=  this.data.year + '-' + this.data.month + '-' + startdate
      let e_date = new Date(this.data.year,this.data.month,this.data.day)
      e_date.setDate(e_date.getDate() + 1)
      let m = e_date.getMonth()
      if(m < 10){
        m = '0' + m
      }
      let d = e_date.getDate()
      if(d < 10){
        d = '0' + d
      }
      let enddate = e_date.getFullYear() + '-' +m + '-' + d
      data = {startdate:startdate,enddate: enddate}
    }
    app.api.getClocklist(data).then(res => {
      if(res.code == 200){
        this.setData({
          clolist : res.rows,
          onSelected:-1
        })
      }
    })
  },
  bindDateChange(e){
    console.log(e)
    let ym =  e.detail.value
    this.setData({
      year : ym.slice(0,ym.indexOf('-')),
      month :  ym.slice(ym.indexOf('-') + 1)
    })
    this.updateDate()
  },
  updateDate(){
    let y = this.data.year
    let m = this.data.month
    let date = new Date(y, m, 0).getDate()
    let ds = ['可选']
    for(let d = 1; d <= date ;d++){
      if(d < 10){
        ds.push('0' + d)
      }else{
        ds.push(d + '')
      }
    }
    this.setData({
      days:ds
    })
    this.search()
  },
  bindDayChange(e){
    this.setData({
      day:this.data.days[e.detail.value]
    })
    this.search()
  },
  ViewImage(e){
    let url = this.data.baseUrl + e.currentTarget.dataset.url
    console.log(url)
    wx.previewImage({
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  clickItem(e){
    this.setData({
      onSelected : this.data.onSelected == e.currentTarget.dataset.index? -1 : e.currentTarget.dataset.index
    })
    
  }
})